package com.czmh.etc.onlinepubsys.services.mingchuang.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czmh.etc.onlinepubsys.entity.mingchuang.Device;
import com.czmh.etc.onlinepubsys.entity.mingchuang.ExchangeCode;
import com.czmh.etc.onlinepubsys.maper.mingchuang.DeviceMapper;
import com.czmh.etc.onlinepubsys.maper.mingchuang.ExchangeCodeMapper;
import com.czmh.etc.onlinepubsys.request.mingchuang.ExchangeCodeRequest;
import com.czmh.etc.onlinepubsys.services.mingchuang.IExchangeCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class ExchangeCodeServiceImpl implements IExchangeCodeService {
    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private ExchangeCodeMapper exchangeCodeMapper;

    @Override
    public List<String> createCode(String dvId, Integer num) {
        Device device = deviceMapper.selectById(dvId);
        List<String> codes = new ArrayList<>();
        int i = 0;
        while (true) {
            String code = UUID.randomUUID().toString()
                    .replaceAll("-", "").substring(0, 12).toUpperCase();

            QueryWrapper<ExchangeCode> wrapper = new QueryWrapper<>();
            wrapper.eq("code", code);
            Integer count = exchangeCodeMapper.selectCount(wrapper);
            if (count > 0) {
                continue;
            }

            ExchangeCode exchangeCode = new ExchangeCode(null, dvId, device.getName(), device.getAmount(),
                    code, new Date(), null, null, 0);
            exchangeCodeMapper.insert(exchangeCode);

            codes.add(code);
            i++;
            if (i == num) {
                break;
            }
        }
        return codes;
    }

    @Override
    public boolean checkExchangeCode(String exchangeCode, String deviceId) {
        QueryWrapper<ExchangeCode> wrapper = new QueryWrapper<>();
        wrapper.eq("code", exchangeCode);
        //wrapper.eq("dv_id", deviceId);
        wrapper.eq("state", 0);
        return exchangeCodeMapper.selectCount(wrapper) > 0;
    }

    @Override
    public IPage<ExchangeCode> query(ExchangeCodeRequest req) {
        IPage<ExchangeCode> page = new Page<>(req.getCurrentPage(), req.getPageSize());
        QueryWrapper<ExchangeCode> wrapper = new QueryWrapper<>();
        String code = req.getCode();
        if (StringUtils.hasLength(code)) {
            wrapper.eq("code", code);
        }

        Integer state = req.getState();
        if (state != -1) {
            wrapper.eq("state", state);
        }

        wrapper.orderByDesc("createTime");
        return exchangeCodeMapper.selectPage(page, wrapper);
    }
}
